package com.zzr.jdk8.source.demo.collection;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author: xiaomi
 * @date: 2021/9/11
 * @description:
 */
public class HashMapDemo {


    /**
     * 当hash 相同进行插入
     */
    @Test
    public void newHashMap() {
        HashMap<String, String> map = new HashMap<>();
        map.put("重地", "a");
        System.out.println("重地".hashCode());
        map.put("通话", "b");
        System.out.println("通话".hashCode());
        System.out.println(map);
    }

    @Test
    public void newCapHashMap() {
        HashMap<String, String> map = new HashMap<>(10);
        map.put("重地", "a");
        
    }

    /**
     * 测试树化：任意桶中元素超过8个，先使用数组扩容机制；直到数组长度超过64，就会进行树化！
     */
    @Test
    public void treeFy() {
        Map map = new HashMap<A, Object>();
        byte b = 1;
        for (int i = 0; i < 20; i++) {
            map.put(new A(i), b);
        }
    }

    /**
     * 添加多个，研究树化
     */
    @Test
    public void addALot() {
        HashMap<Integer, Integer> map = new HashMap<>();
        int num = 1;
        for (int i = 0; i < 30; i++) {
            map.put(num, i);
            num <<= 1;
        }
        System.out.println("map = " + map);
        int key = 4096;
        Integer value = map.get(key);
    }

    /**
     * 保证相同的 hashCode
     */
    static class A {
        private int num;

        public A(int num) {
            this.num = num;
        }

        @Override
        public int hashCode() {
            return 100;
        }
    }
}
